#include "ControlPanel.h"
#include "MainWindow.h"

ControlPanel::ControlPanel(QWidget *parent) : QWidget(parent)
{
    backgroundColorPanel = new ColorPanel(QColor(0, 0, 255), this);
    backgroundColorPanel -> SetTitle("背景色");

    frameControlLabel = new QLabel("帧控制", this);
    frameControlLabel -> setAlignment(Qt::AlignmentFlag::AlignCenter);
    frameController = new SpinBoxWithSlider("帧号");
    frameControlLayout = new QVBoxLayout();
    frameControlLayout -> addWidget(frameControlLabel);
    frameControlLayout -> addWidget(frameController);
    frameControlLayout -> setAlignment(Qt::AlignmentFlag::AlignCenter);

    layout = new QVBoxLayout(this);
    layout -> addWidget(backgroundColorPanel);
    layout -> addLayout(frameControlLayout);
    setLayout(layout);

    frameController -> SetMaxValue(0);
    frameController -> setDisabled(true);
	backgroundColorPanel -> setDisabled(true);

    connect(backgroundColorPanel, SIGNAL(ValueChanged(const QColor&)), this, SLOT(OnBackgroundColorChange(const QColor&)));
    connect(frameController, SIGNAL(ValueChanged(int)), this, SLOT(OnIndexChange(int)));
}

ControlPanel::~ControlPanel()
{
    delete backgroundColorPanel;
    delete frameController;
    delete frameControlLabel;
    delete layout;
    delete frameControlLayout;
}

void ControlPanel::OnMcsUpdate()
{
    frameController -> SetValue(0);
    auto mcs = MainWindow::Instance() -> GetMcs();
    if(mcs == nullptr)
    {
        frameController -> SetMaxValue(0);
        frameController -> setDisabled(true);
		backgroundColorPanel -> setDisabled(true);
        return;
    }
    frameController -> SetMaxValue(mcs -> GetMcsHead().GetFrameCount() - 1);
    frameController -> setDisabled(false);
	backgroundColorPanel -> setDisabled(false);
}

void ControlPanel::OnBackgroundColorChange(const QColor& c)
{
	backgroundColor = c;
	emit(StatusUpdate(backgroundColor, index));
}

void ControlPanel::OnIndexChange(int i)
{
	this->index = i;
	emit(StatusUpdate(backgroundColor, i));
}
